from django.shortcuts import render, redirect
from app01 import models
from app01.utils.forms import UserLoginModelForm, ManagerLoginModelForm, UserAddModelForm, ManagerAddModelForm
from app01.utils.encrypt import md5


def manager_register(request):
    """管理员注册"""
    title = '管理员注册界面'
    if request.method == 'GET':
        form = ManagerAddModelForm()
        content = {
            'form': form,
            'title': title
        }
        return render(request, 'user_register.html', content)
    form = ManagerAddModelForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect('/manager/user/list/')
    content = {
        "form": form,
        'title': title,
    }
    return render(request, 'user_register.html', content)


def index(request):
    """首页"""
    print(md5('3141592hzy'))
    return render(request, 'index.html')


def user_register(request):
    """用户注册"""
    title = '用户注册界面'
    if request.method == 'GET':
        form = UserAddModelForm()
        content = {
            'form': form,
            'title': title
        }
        return render(request, 'user_register.html', content)
    form = UserAddModelForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect('/manager/user/list/')
    content = {
        "form": form,
        'title': title,
    }
    return render(request, 'user_register.html', content)


def manager_login(request):
    """管理员登录"""
    title = '管理员登录页面'
    if request.method == "GET":
        form = ManagerLoginModelForm()
        content = {
            'form': form,
            'title': title
        }
        return render(request, 'manager_login.html', content)
    form = ManagerLoginModelForm(data=request.POST)
    print(form.is_valid())
    if form.is_valid():
        admin_object = models.Manager.objects.filter(**form.cleaned_data).first()
        print(admin_object)
        if not admin_object:
            form.add_error("Manager_password", "管理员密码错误")
            content = {
                'form': form,
                'title': title
            }
            return render(request, 'manager_login.html', content)
        request.session["info"] = {
            'id': admin_object.id,
            'name': admin_object.Manager_name,
            'type': 'manager'
        }
        return redirect('/manager/user/list/')
    content = {
        'form': form,
        'title': title
    }
    return render(request, 'manager_login.html', content)


def user_login(request):
    """用户登录"""
    title = '用户登录页面'
    if request.method == "GET":
        form = UserLoginModelForm()
        content = {
            'form': form,
            'title': title
        }
        return render(request, 'user_login.html', content)
    form = UserLoginModelForm(data=request.POST)
    # print(form.is_valid())
    if form.is_valid():
        admin_object = models.User.objects.filter(**form.cleaned_data).first()
        if not admin_object:
            form.add_error("User_password", "用户名密码错误")
            content = {
                'form': form,
                'title': title
            }
            return render(request, 'user_login.html', content)
        request.session["info"] = {
            'id': admin_object.id,
            'name': admin_object.User_name,
            'type': 'user',
        }
        return redirect('/user/function/')
    content = {
        'form': form,
        'title': title
    }
    return render(request, 'user_login.html', content)


def loginout(request):
    """注销"""
    request.session.clear()
    return redirect("/index/")
